<template>
  <!-- 中药处方打印选择 -->
  <el-dialog title="中药处方打印" :visible="value" width="700px" custom-class="dialog-box" @close="onClose" @open="onOpen" :close-on-click-modal="false" :append-to-body="true">
    <table class="table-box">
      <thead>
        <tr>
          <th>
            <el-checkbox :value="checkAll" @change="onCheckAll"></el-checkbox>
          </th>
          <th>药品名称</th>
          <th>剂量</th>
          <th>总量</th>
          <th>单价</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item,index) in tableList" :key="index">
          <td>
            <el-checkbox v-model="item.check"></el-checkbox>
          </td>
          <td>{{item.drugName}}</td>
          <td>{{item.oneDose}}{{item.oneDoseUnit|changeAssist(unitEnum)}}</td>
          <td>{{item.useTotal}}{{item.totalUnit|changeAssist(unitEnum)}}</td>
          <td>￥{{item.sellMoney}}</td>
        </tr>
      </tbody>
    </table>
    <div v-if="tableList.length==0" style="color:#999;padding:10px 0;text-align:center;">
      <i class="iconfont iconwushuju" style="font-size:40px;"></i>
      <div>暂无数据</div>
    </div>
    <br>
    <div>每次{{useObj.dailyDose}}剂，{{useObj.usage|changeAssist(useEnum)}}，{{useObj.frequency|changeAssist(frequencyEnum)}}，{{useObj.totalDose}}付，共{{useObj.day}}天</div>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="loading" type="primary" @click="onSubmit">确 定</el-button>
    </span>
    <iframe ref="iframe1" frameborder="0" height="0px" width="0px" src="/print/newtraditiondurg.html"></iframe>
  </el-dialog>
</template>
<script>
export default {
  props: [
    "value",
    "cureId",
    "patientInfo",
    "list",
    "useObj",
    "professionalDeptName",
    "doctorName",
    "cureFirstVisitRecord"
  ],
  components: {},
  data() {
    return {
      loading: false,
      tableList: [],
      tabIndex: "0"
    };
  },
  computed: {
    //剂量单位
    unitEnum() {
      return this.$store.state.doctorMZ.enum.doseUnitEnum;
    },
    //用法
    useEnum() {
      return this.$store.state.doctorMZ.enum.useEnum;
    },
    //频次
    frequencyEnum() {
      return this.$store.state.doctorMZ.enum.frequencyEnum;
    },
    checkAll() {
      const th = this;
      let bo = true;
      let index = th.tableList.findIndex(d => {
        return !d.check;
      });
      if (index != -1) bo = false;
      return bo;
    }
  },
  methods: {
    async onSubmit() {
      let th = this;
      if (!th.tableList) return;
      let json = {
        hospital: {
          name: th.adminUser.hospitalName,
          logo: th.adminUser.printLogo
        },
        enum: {
          unitEnum: th.unitEnum,
          useEnum: th.useEnum,
          frequencyEnum: th.frequencyEnum
        },
        patientInfo: th.patientInfo,
        professionalDeptName: th.professionalDeptName,
        useObj: th.useObj,
        doctorName: th.doctorName,
        // allergy: th.tableList.allergy,
        // basePrintVO: th.tableList.basePrintVO,
        firstVisit: th.cureFirstVisitRecord,
        drug: []
      };
      //判断是否选择
      th.tableList.forEach(d => {
        if (d.check) {
          json.drug.push(d);
        }
      });
      if (json.drug.length == 0) {
        th.$message.warning("请勾选药品");
        return;
      }
      th.loading = true;
      let res1 = await th.$api.findCureCode({ cureId: th.cureId });
      if (!res1.executed) {
        return;
      }
      th.loading = false;
      json.barCode = th.$config.imageUrl + res1.dataBody.barCode;
      th.$refs["iframe1"].contentWindow.postMessage(
        json,
        `${window.location.origin}/print/newtraditiondurg.html`
      );
      th.onClose();
    },
    //全选
    onCheckAll(obj) {
      let th = this;
      th.tableList.forEach(d => {
        th.$set(d, "check", obj);
      });
    },
    getData() {
      const th = this;
      let arr = [];
      th.list.forEach(d => {
        let j = JSON.parse(JSON.stringify(d));
        j.check = true;
        arr.push(j);
      });
      th.tableList = arr;
    },
    onOpen() {
      let th = this;
      th.getData();
    },
    onClose() {
      let th = this;
      th.tableList = [];
      th.$emit("input", false);
    }
  }
};
</script>